package com.ld.security.shop.crawler.modules.re.crawler.java1234;

import com.ld.security.shop.crawler.modules.re.constant.CrawlerConstant;
import com.ld.security.shop.crawler.modules.re.crawler.BaseCrawler;
import com.ld.security.shop.crawler.modules.re.entity.ReDetails;
import com.ld.security.shop.crawler.modules.re.entity.RePage;
import com.ld.security.shop.crawler.modules.re.service.IReDetailsService;
import com.ld.security.shop.crawler.modules.re.service.IRePageService;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;

@Component
public class Java1234RePageCrawler extends BaseCrawler<RePage> {

    @Autowired
    IRePageService iRePageService;

    @Autowired
    IReDetailsService iReDetailsService;

    @Autowired
    Java1234ReDetailsCrawler java1234ReDetailsCrawler;

    public Java1234RePageCrawler() {
        super( CrawlerConstant.projectCode.java1234);
    }

    @Override
    public RePage dataCrawler(String url) {
        RePage page = super.dataCrawler(url);



        Document document =this.parse(page.getContent());

        for (Element element : document.select(".data_list")) {

            Element title = element.select(".data_list_title").first();
                if ("最新博客".equals(title.text())) {
                    page.setContent(element.html());
                    continue;
                }
        }
        
        Elements pages = document.select(".pagination-sm > li a");
        for (Element a : pages) {


            String text = a.text();
            String href = a.attr("href");

            if ("上一页".equals(text)) {
                page.setPreUrl(href);
            } else if ("下一页".equals(text)) {
                page.setNextUrl(href);
            }
        }

        iRePageService.save(page);

        LinkedList linkedList =   new LinkedList<>();

        Elements elements = document.select(".data_list");
        for (Element element : elements) {
            Element title = element.select(".data_list_title").first();

            if ("最新博客".equals(title.text())) {
                Elements elements2 = element.select("li");
                for (Element element1 : elements2) {
                    Element a = element1.select(".title>a").first();

                    String href = a.attr("href");
                    ReDetails o = java1234ReDetailsCrawler.dataCrawler(href);

                    linkedList.add(o);
                }
            }
        }
        iReDetailsService.saveBatch(linkedList);

        return page;
    }





}

